<?php

namespace interface\v1;

/*
*个人中心接口类
*/

class myAppClass extends index
{
    private const title = '个人中心接口类';
	/*个人中心首页*/
	public function home()
	{
		$this->code         = 1;
        $source = $this->header['Source'];
        $company = query('company',"coid = '".self::$coid."'");
        $companyName = match ($source){
            'crm'=>$company['name'],
            default=>para('companyName'),
        };
		$data['result']     = [
			'name'       => $this->control['name'] ?? '',
			'ico'        => self::fillSrc($this->control['ico'] ?? '', $this->control['updateTime'] ?? ''),
			'job'        => $this->controlJob['name'] ?? '',
			'department' => $this->controlDepartment['name'] ?? '',
			'company'    => $companyName,
			'hotline'    => para('hotline'),
			'userName'   => self::$user['userName'] ?? ''
			//            'corpId'=>para('qyWeChatCorpid'),
			//            'serviceUrl'=>para('serviceUrl'),
		];
		$this->resp['data'] = $data;
	}

    /**
     * 通知设置
     */
	public function setting(){
        $source = $this->header['Source'];
        $data= [];
        if ($source =='oa'){
            $user_sertting = query('user_setting',"stid = '" . self::$stid . "' and type = 'OA通知设置'");
            $data = json_decode($user_sertting['value'],true);
        }
        $this->code = 1;
        $this->resp['data'] = $data;
    }
    /**
     * OA消息设置
     */
	public function oaMessageSetting(){
	    $isOpen = $this->post('isOpen');
	    $info = query('user_setting',"stid = '" . self::$stid . "' and type = 'OA通知设置'");
	    if (empty($info['id'])){
	        $this->resp['warn'] = '未找到此设置';
        }else{
            $setting = json_decode($info['value'],true);
            $setting['message'] = $isOpen;
            $warn = static::update(array('table'=>'user_setting','field'=>['value'=>json_encode($setting,320)],'where'=>"id = '$info[id]'"));
            if ($warn =='更新成功'){
                $this->code = 1;
                $this->resp['warn'] ='设置成功';
            }else{
                $this->resp['warn'] ='设置失败';
            }
        }
    }
	
	/**
	 * 关于
	 */
	public function about()
	{
		$this->code         = 1;
		$type = $this->post('type');
		switch ($type){
            case '用户服务及隐私协议':
                $content            = query('document', "title = '用户服务及隐私协议' and state = '已发布' order by time desc");
                break;
            default:
                $content            = query('document', "title = '公司介绍' and state = '已发布' order by time desc");
                break;
        }

		$data['result']     = [
			'text' => html_entity_decode($content['content'], ENT_QUOTES),
		];
		$this->resp['data'] = $data;
	}
	
	/**
	 * 更新日志
	 */
	public function updateLog()
	{
		$this->code     = 1;
		$data['result'] = [];
		$type           = match ($this->post('miniProgram')) {
			'crm', 'oa', 'erp', 'pda' => $this->post('miniProgram'),
			default                   => '其他',
		};
		$all            = find([
			'table' => 'document',
			'where' => "title = '更新日志' and dcid in (select id from document_catalog where name = '{$type}小程序端') and state = '已发布' order by time desc",
		]);
		foreach ($all['array'] as $val) {
			$data['result'][] = ['text' => html_entity_decode($val['content'], ENT_QUOTES)];
		}
		$this->resp['data'] = $data;
	}
	
	/**
	 * 退出登录
	 *
	 * @return void
	 */
	public function loginOut()
	{
		$token = $this->header['Auth'];
		//更新时间
		if ($token) {
			$this->rSet($token, null);
			$this->code         = 1;
			$this->resp['warn'] = '退出成功';
		}
	}
	
}